const Uniq = require("../template")

function testUniqPerformance() {
  console.log(
    "1. 数组去重性能测试 - 大量数据去重:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i % 1000)
      const start = performance.now()
      const result = Uniq.uniq(largeArray)
      const end = performance.now()
      return (end - start) < 100 && result.length === 1000
    })()
  )

  console.log(
    "2. 数组去重性能测试 - 内存使用:",
    (() => {
      const initialMemory = process.memoryUsage().heapUsed
      const largeArray = Array.from({ length: 10000 }, (_, i) => i % 1000)
      const result = Uniq.uniq(largeArray)
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      return memoryIncrease < 1024 * 1024 // 内存增长应该小于1MB
    })()
  )

  console.log(
    "3. 数组去重性能测试 - 自定义比较函数:",
    (() => {
      const largeArray = Array.from({ length: 5000 }, (_, i) => ({ id: i % 1000, value: i }))
      const start = performance.now()
      const result = Uniq.uniqBy(largeArray, 'id')
      const end = performance.now()
      return (end - start) < 50 && result.length === 1000
    })()
  )

  console.log(
    "4. 数组去重性能测试 - 条件去重:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = Uniq.uniqWith(largeArray, (a, b) => a % 2 === b % 2)
      const end = performance.now()
      return (end - start) < 50 && result.length === 2
    })()
  )

  console.log(
    "5. 数组去重性能测试 - 深度去重:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => [i % 100, i % 100, i % 100])
      const start = performance.now()
      const result = Uniq.uniqDeep(largeArray, 1)
      const end = performance.now()
      return (end - start) < 50 && result.length === 100
    })()
  )

  console.log(
    "6. 数组去重性能测试 - 排序去重:",
    (() => {
      const largeArray = Array.from({ length: 5000 }, () => Math.floor(Math.random() * 1000))
      const start = performance.now()
      const result = Uniq.uniqSorted(largeArray, (a, b) => a - b)
      const end = performance.now()
      return (end - start) < 100 && result.length <= 1000
    })()
  )

  console.log(
    "7. 数组去重性能测试 - 类型去重:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i % 2 === 0 ? i : i.toString())
      const start = performance.now()
      const result = Uniq.uniqByType(largeArray, 'number')
      const end = performance.now()
      return (end - start) < 50 && result.length === 5000
    })()
  )

  console.log(
    "8. 数组去重性能测试 - 转换去重:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = Uniq.uniqWithTransform(largeArray, x => x % 100)
      const end = performance.now()
      return (end - start) < 50 && result.length === 100
    })()
  )

  console.log(
    "9. 数组去重性能测试 - 验证去重:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = Uniq.uniqWithValidation(largeArray, { min: 0, max: 10000 })
      const end = performance.now()
      return (end - start) < 50 && result.length === 10000
    })()
  )

  console.log(
    "10. 数组去重性能测试 - 重复数据:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, () => 1)
      const start = performance.now()
      const result = Uniq.uniq(largeArray)
      const end = performance.now()
      return (end - start) < 50 && result.length === 1
    })()
  )

  console.log(
    "11. 数组去重性能测试 - 无重复数据:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = Uniq.uniq(largeArray)
      const end = performance.now()
      return (end - start) < 50 && result.length === 10000
    })()
  )

  console.log(
    "12. 数组去重性能测试 - 混合类型:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => 
        i % 3 === 0 ? i : i % 3 === 1 ? i.toString() : { id: i }
      )
      const start = performance.now()
      const result = Uniq.uniq(largeArray)
      const end = performance.now()
      return (end - start) < 100 && result.length <= 10000
    })()
  )

  console.log(
    "13. 数组去重性能测试 - 嵌套对象:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => ({
        id: i % 100,
        data: { value: i, nested: { deep: i % 10 } }
      }))
      const start = performance.now()
      const result = Uniq.uniqBy(largeArray, 'id')
      const end = performance.now()
      return (end - start) < 50 && result.length === 100
    })()
  )

  console.log(
    "14. 数组去重性能测试 - 并发处理:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i % 1000)
      const start = performance.now()
      
      const promises = []
      for (let i = 0; i < 10; i++) {
        promises.push(Promise.resolve().then(() => Uniq.uniq(largeArray)))
      }
      
      return Promise.all(promises).then(results => {
        const end = performance.now()
        return (end - start) < 200 && results.every(r => r.length === 1000)
      })
    })()
  )

  console.log(
    "15. 数组去重性能测试 - 内存泄漏检测:",
    (() => {
      const initialMemory = process.memoryUsage().heapUsed
      
      for (let i = 0; i < 100; i++) {
        const largeArray = Array.from({ length: 1000 }, (_, j) => j % 100)
        const result = Uniq.uniq(largeArray)
      }
      
      if (global.gc) {
        global.gc()
      }
      
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      
      return memoryIncrease < 1024 * 1024 // 内存增长应该小于1MB
    })()
  )
}

testUniqPerformance()
